package com.maker.generator.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.maker.generator.entity.MetaGenObj;

/**
 * 数据库与JAVA对象的转换工具类
 * 
 */
public class TransUtil {
	
	
	
	
	// 将数据库表转成java对象名
	public static String tableName2ObjectName(String tableName,String prefix) {
		String[] labels = tableName.split("_");
		String objName = "";
		for(int i=0;i<labels.length;i++){
			if(labels[i].toLowerCase().equals(prefix) || labels[i].toUpperCase().equals(prefix)){
				//过滤掉前缀
			}else{
				objName+=upperCase(labels[i].toLowerCase());
			}
		}
		return objName;
	}
	//从JAVA对象生成表名
	public static String obj2TableName(MetaGenObj obj){
		String tableName = obj.getObjName().toLowerCase();
		tableName = getShortPackageName(obj.getObjPackageName())+"_"+tableName;
		return tableName;
	}
	//从包名中取最后一截包名的前四位。
	public static String getShortPackageName(String packageName){
		String shortPackageName = "";
		String[] str =packageName.split("\\.");
		shortPackageName = str[str.length-1];
		if(shortPackageName.length()>4){
			shortPackageName = shortPackageName.substring(0,4);
		}
		return shortPackageName.toLowerCase();
	}
	
	// 将数据库表字段名转成java属性名
		public static String fieldName2AttrName(String fieldName) {
			String[] labels = fieldName.split("_");
			String attrName = "";
			for(int i=0;i<labels.length;i++){
				if(i==0){
					//属性名小写开头
					attrName = labels[i].toLowerCase();
				}else{
					attrName+=upperCase(labels[i].toLowerCase());
				}
			}
			return attrName;
		}
	
	public static String oracleFieldType2JavaShortType(String fieldType){
		String javaType = "";
		switch(fieldType){
		case "VARCHAR2":
			javaType = "String";
			break;
		case "NUMBER":
			javaType = "Long";
			break;	
		default :
			break;
		}
		return javaType;
	}
	
	public static String oracleFieldType2JavaType(String fieldType){
		String javaType = "";
		switch(fieldType){
		case "VARCHAR2":
			javaType = "java.lang.String";
			break;
		case "NUMBER":
			javaType = "java.lang.Long";
			break;	
		default :
			break;
		}
		return javaType;
	}
	public static String javaType2OracleFieldType(String javaType){
		String fieldType = "";
		switch(javaType){
		case "java.lang.String":
			fieldType = "VARCHAR2";
			break;
		case "java.lang.Long":
			fieldType = "NUMBER";
			break;	
		default :
			fieldType = "NUMBER";
			break;
		}
		return fieldType;
	}
	public static String javaType2MysqlFieldType(String javaType){
		String fieldType = "";
		switch(javaType){
		case "java.lang.String":
			fieldType = "VARCHAR2";
			break;
		case "java.lang.Long":
			fieldType = "NUMBER";
			break;	
		default :
			fieldType = "NUMBER";
			break;
		}
		return fieldType;
	}
	
	//首字母大写
	public static String upperCase(String str) {  
	    char[] ch = str.toCharArray();  
	    if (ch[0] >= 'a' && ch[0] <= 'z') {  
	        ch[0] = (char) (ch[0] - 32);  
	    }  
	    return new String(ch);  
	}
	//首字母小写
		public static String lowerCase(String str) {  
		    char[] ch = str.toCharArray();  
		    if (ch[0] >= 'A' && ch[0] <= 'Z') {  
		        ch[0] = (char) (ch[0] + 32);  
		    }  
		    return new String(ch);  
		}
	/**
	 * 因为
	 * 使用java中的转义符"\r\n": 
	 * windows下的文本文件换行符:\r\n 
	 * linux/unix下的文本文件换行符:\r 
	 * Mac下的文本文件换行符:\n
	 * 所以,先将\r换成\n，然后将\n\n替换为\n,最后用\n分割。可解决各种输入源问题
	 * @param word
	 * @return
	 */
	public static List<String> splitWord(String word){
		List<String>  rows = new ArrayList<String>();
		word = word.replaceAll("\r", "\n");
		word = word.replaceAll("\n\n", "\n");
		String[] words = word.split("\n");
		rows = new ArrayList<String>(Arrays.asList(words));
		return rows;	
	}
	/**
	 * 将string的List 拼接成代码片段
	 * @param sourceList
	 * @param lineSeparator
	 * @return
	 */
	public static String arrayList2Code(List<String> sourceList,String lineSeparator){
		
		if(lineSeparator == null || "".equals(lineSeparator)){
			lineSeparator = System.getProperty("line.separator");
		}
		return arrayList2String(sourceList,lineSeparator);
	}
	/**
	 * 把string的List，用拼接符拼接成一个String。
	 * @param sourceList
	 * @param lineSeparator
	 * @return
	 */
	public static String arrayList2String(List<String> sourceList,String separator){
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<sourceList.size();i++){
			sb.append(sourceList.get(i));
			if(i != (sourceList.size()-1)){
				sb.append(separator);
			}
		}
		return sb.toString();
	}
	
	



}